package com.logos.musicframe.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.logos.musicframe.req.PlaylistReq;
import com.logos.musicframe.req.UsersReq;
import com.logos.musicframe.resp.PlaylistResp;
import com.logos.musicframe.result.Result;
import com.logos.musicframe.service.PlaylistService;
import com.logos.musicframe.utils.UserInfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 歌单表(Playlist)表控制层
 *
 * @author logos
 * @since 2025-06-14 21:28:44
 */
@Component
@Slf4j
public class PlaylistController {

    @Autowired
    private PlaylistService playlistService;

    @Autowired
    private UserInfoUtil userInfoUtil;

    /**
     * 查询当前用户创建的歌单
     */
    public Result<List<PlaylistResp>> queryByUserId() {
        try {
            Integer userId = userInfoUtil.getUserId();
            return Result.ok(playlistService.queryCreated(userId));
        } catch (Exception e) {
            log.error("PlaylistController.queryByUserId.error{}", e.getMessage(), e);
            return Result.fail("查询歌单失败");
        }
    }

    /**
     * 创建歌单
     */
    public Result<PlaylistResp> add(PlaylistReq playlistReq) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PlaylistController.add.playlistReq{}", JSON.toJSONString(playlistReq));
            }
            Preconditions.checkNotNull(playlistReq.getName(), "歌单名称不能为空");
            Preconditions.checkNotNull(playlistReq.getDescription(), "歌单描述不能为空");
            Preconditions.checkNotNull(playlistReq.getCoverFile(), "歌单封面不能为空");
            playlistReq.setCreatedBy(userInfoUtil.getUserId());
            PlaylistResp playlistResp = playlistService.add(playlistReq);
            return Result.ok(playlistResp);
        } catch (Exception e) {
            log.error("PlaylistController.add.error{}", e.getMessage(), e);
            return Result.fail("创建歌单失败");
        }
    }

    /**
     * 查询歌单通过歌单id
     * @param id
     * @return
     */
    public Result<PlaylistResp> queryById(Long id) {
        try {
            log.info("PlaylistController.queryById.id:{}", id);
            PlaylistReq playlistReq = new PlaylistReq().setId(id);
            PlaylistResp playlistResp = playlistService.queryById(playlistReq);
            return Result.ok(playlistResp);
        } catch (Exception e) {
            log.error("PlaylistController.queryById.error{}", e.getMessage(), e);
            return Result.fail("查询歌单失败");
        }
    }

    /**
     * 查询所有歌单
     */
    public Result<List<PlaylistResp>> queryAll() {
        try {
            List<PlaylistResp> playlistRespList = playlistService.queryAll();
            return Result.ok(playlistRespList);
        } catch (Exception e) {
            log.error("PlaylistController.queryAll.error{}", e.getMessage(), e);
            return Result.fail("查询所有歌单失败");
        }
    }

    /**
     * 主页推荐歌单
     * @return
     */
    public Result<List<PlaylistResp> > getRecommendPlaylist() {
        try {
            List<PlaylistResp> playlistRespList = playlistService.getRecommendPlaylist();
            return Result.ok(playlistRespList);
        } catch (Exception e) {
            log.error("PlaylistController.getRecommendPlaylist.error{}", e.getMessage(), e);
            return Result.fail("查询推荐歌单失败");
        }
    }

}

